<template>
  <card class="card" title="登录">
    <div>
      <form @submit.prevent>
        <div class="row">
          <div class="col-md-11">
            <fg-input type="text" label="用户名" placeholder="用户名" v-model="user.username">
            </fg-input>
          </div>
        </div>

        <div class="row">
          <div class="col-md-11">
            <fg-input type="password" label="密码" placeholder="密码" v-model="user.pwd">
            </fg-input>
          </div>
        </div>

        <div class="row">

        </div>
        <div class="text-center">
          <p-button round @click.native="login">
            登录
          </p-button>
          <span class="col=md-2">&emsp;</span>
          <p-button round @click.native="registry">
            注册
          </p-button>
        </div>
        <div class="clearfix"></div>
      </form>
    </div>
  </card>
</template>
<script>
import NotiTem from "../Notifications/NotificationTemplate";
import axios from 'axios'
import CryptoJS from "crypto-js"
import GlobalVars from "../../plugins/globalVars"

export default {
  data() {
    return {
      type: ["", "success", "warning", "danger"],
      notifications: {
        topCenter: false,
      },
      user: {
        username: "",
        pwd: "",
        state: "NO"
      },

    };
  },
  methods: {

    postData(url, data) {
      return new Promise((reject) => {
        axios.post(url, data)
          .then(response => {
            this.reload(response.data)
            this.$store.state.name1=response.data.username
            this.$store.state.quan=response.data.email
            this.$store.state.text=response.data.aboutme
          })
          .catch(error => {
            reject(error);
          });
      });
    },
    login() {
      this.postData(GlobalVars.Url+`/stats/login?uname=${this.user.username}&password=${CryptoJS.MD5(this.user.pwd).toString()}`, {

      }).catch(error => { this.reject2(error) })
      
    },
    reload(resp) {
      console.log(resp)
      if (resp.username == this.user.username) {
        this.$store.state.nowId = resp.id,
        this.$store.state.nowUsername = resp.username,
        this.$store.state.nowStatus = resp.status,
        this.$store.state.nowEmail = resp.email,
        this.$store.state.nowType = resp.type,
        this.$store.state.nowLocation = resp.location,
        this.$store.state.nowClasswork = resp.classwork,
        this.$store.state.nowPhone = resp.phone,
        this.$store.state.nowClassid =resp.classid,
        this.$store.state.nowAboutme = resp.aboutme,
        sessionStorage.setItem("logStatu", "OK")
        setTimeout(this.$parent.a, 400)
        this.$store.state.imTxt = "登陆成功  -  "
        this.$store.state.sufTxt = "改变，从现在开始；成功，由今天起步"
        this.notifyVue("bottom", "right", 1);
      } else {
        this.$store.state.imTxt = "登陆失败  -  "
        this.$store.state.sufTxt = "无数次艰辛地尝试并不一定能够收获成功，放弃尝试，却意味着永远放弃成功。"
        this.notifyVue("bottom", "right", 2)
      }
    },
    reject2(error) {

      this.$store.state.imTxt = `网络错误  -  ${error}`
      this.notifyVue("bottom", "right", 3)

    },


    registry() {
      this.$parent.b()
    },
    notifyVue(verticalAlign, horizontalAlign, color) {
      // const color = Math.floor(Math.random() * 4 + 1);
      this.$notify({
        component: NotiTem,
        icon: "ti-gift",
        horizontalAlign: horizontalAlign,
        verticalAlign: verticalAlign,
        type: this.type[color],
      });
    },
  },
};
</script>
<style></style>
  